import sys
import os
import SCons.Node
import SCons.Node.FS

cflags = os.environ.get('CFLAGS','-Wall -O2').split()
cxxflags = os.environ.get('CXXFLAGS','-Wall -O2').split()
cppflags = os.environ.get('CPPFLAGS','').split()
ldflags = os.environ.get('LDFLAGS','').split()

# Comamnd line parsing
opts = Variables()
opts.Add(PathVariable('toolchain','the prefix of a toolchain','',PathVariable.PathAccept))
opts.Add(PathVariable('toolchain_dir','where the executables of the toolchain were installed','/',PathVariable.PathIsDir))

build_mode = 'release'

platform = 'default'

#if build_mode == 'release':
#	env.Append(LINKFLAGS='-s')
#else:
#	cxxflags.append('-g')
#	cppflags.append('-g')
#	env.Append(CPPFLAGS=' -DDEBUG')

env = Environment( ENV={'PATH':os.environ['PATH']}, tools = [platform], toolpath = ['.'], CFLAGS=cflags, CXXFLAGS = cxxflags, CPPFLAGS = cppflags, 
	LINKFLAGS = ldflags, variables = opts)

toolchain_dir = env['toolchain_dir']
if not toolchain_dir == '/':
	toolchain_bin = os.path.join(toolchain_dir,'bin')
	if not os.path.isdir(toolchain_bin):
		print 'Error: invalid toolchain_dir variable'
		sys.exit(1)
	env.Append(ENV = {'PATH':os.environ['PATH']+':'+toolchain_bin})

toolchain = env['toolchain']
toolchain = toolchain.rstrip('-')
if not toolchain == '':
	env['CC'] = toolchain+'-gcc'
	env['CXX'] = toolchain+'-g++'
	env['CPP'] = toolchain+'-cpp'
	env['AR'] = toolchain+'-ar'
	env['RANLIB'] = toolchain+'-ranlib'
	env['AS'] = toolchain+'-as'


src=['frodoII.c']

frodoII = env.Program('frodoII', src)
env.Decider('make')
build = env.Alias('build', [frodoII])
env.Default(build)

